package me.bincker.formwork.web.core.config

import me.bincker.formwork.web.core.security.MethodSecurityMetadataSource
import me.bincker.formwork.web.core.service.IUserDetailsService
import org.springframework.context.annotation.Bean
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder

/**
 * 安全配置
 */
@EnableGlobalMethodSecurity
class MethodSecurityConfiguration(
        val userDetailsService: IUserDetailsService,
        val methodSecurityMetadataSource: MethodSecurityMetadataSource
): GlobalMethodSecurityConfiguration() {

    @Bean
    fun passwordEncoder() = BCryptPasswordEncoder(12)

    override fun configure(auth: AuthenticationManagerBuilder) {
        auth.userDetailsService(userDetailsService)
    }

    override fun customMethodSecurityMetadataSource(): MethodSecurityMetadataSource {
        return methodSecurityMetadataSource
    }
}